package jsc.acid;

import java.io.File;
import java.io.IOException;

import jsc.base.TextFileReader;
import jsc.base.TextFileWriter;
import jsc.structureDeDonnees.ABRString;
import jsc.structureDeDonnees.StructureDico;
import jsc.structureDeDonnees.TreeSetString;

/**
 * This class contains a list of words (store in an ABR or a TreeSet) and provides methods to tell 
 * whether it contains a specified word or not.
 * 
 * A dictionnary is a first of all a file and this is why you can not create a dictionnary without an attached file,
 * however as you can add words to the dictionnary, you can also add the words to the file (method "save()")
 * 
 * @author Alec Ermis, Maxime Rihouey
 *
 */
public class Dictionary {

	private StructureDico arbre; // can be an ABRString or TreeSetString
	private String name;
	private File file; // there have been more than one modification since the last saving
	private boolean modified; // there have been at least one modification since the instanciation of the Dictionnary (with or without saving)
	private boolean savedSinceLastModif;

	// constructeurs
	private Dictionary(TextFileReader reader, File file, boolean useABR){
		if(useABR){
			arbre = new ABRString(reader.readerToArray());
		}else{
			arbre = new TreeSetString(reader.readerToArray());
		}
		name = file.getName();
		this.file = file;
		modified = false;
		savedSinceLastModif = true;
	}

	public Dictionary(File fichier, boolean useABR) throws IOException{
		this(new TextFileReader(fichier),fichier, useABR);
	}

	// getters
	public String getName(){
		return name;
	}

	public boolean hasBeenModified(){
		return modified;
	}

	// display
	public String toString(){
		return arbre.toString();
	}

	// methods
	public boolean hasBeenSaved(){
		return savedSinceLastModif; 
	}

	/**
	 * test if the dictionary contains the specified String
	 */
	public boolean contains(String s){
		return arbre.existe(s);
	}

	/**
	 * add the String the data structure of the dictionary
	 * @param s
	 */
	public void add(String s){
		arbre.ajouter(s);
		modified = true;
		savedSinceLastModif = false;
	}

	/**
	 * rewrite the file attached to the dictionnary with all the modification done
	 * doesn't do anything if the dictionary haven't been modified. 
	 */
	public void save(){
		if(modified){
			try {
				TextFileWriter writer = new TextFileWriter(this.file);
				String[] tab = this.arbre.tableau();
				for(int i = 0; i < tab.length; i++){
					writer.write(tab[i]);
					writer.newLine();
				}
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		savedSinceLastModif = true;
	}

	/*
	 * Test
	 */
	public static void main(String[] args) {

		System.out.println("Avec ABR");
		boolean useABR = true;
		System.out.println("###################### Test Positif ################");
		try {

			// test positif
			Dictionary dictionary = new Dictionary(new File("dicts/dictTest.jsc"),useABR);
			//System.out.println(dictionary);
			System.out.println(dictionary.contains("ABNF"));
			System.out.println(dictionary.contains("abalones"));
			dictionary.add("test");
			System.out.println(dictionary.contains("test"));

			//dictionary.save();


		} catch (IOException e) {
			System.out.println("Impossible de trouver le fichier.");
		}

		System.out.println("###################### Test Negatif ################");
		try {

			// test negatif
			Dictionary dictionary = new Dictionary(new File("dicts/dictTest2.jsc"),useABR);
			//System.out.println(dictionary);
			System.out.println(dictionary.contains("ABNF"));
			System.out.println(dictionary.contains("abalones"));
			dictionary.add("test");
			System.out.println(dictionary.contains("test"));

			//dictionary.save();


		} catch (IOException e) {
			System.out.println("Impossible de trouver le fichier.");
		}

		System.out.println("Sans ABR");
		useABR = false;
		System.out.println("###################### Test Positif ################");
		try {

			// test positif
			Dictionary dictionary = new Dictionary(new File("dicts/dictTest.jsc"),useABR);
			//System.out.println(dictionary);
			System.out.println(dictionary.contains("ABNF"));
			System.out.println(dictionary.contains("abalones"));
			dictionary.add("test");
			System.out.println(dictionary.contains("test"));

			//dictionary.save();


		} catch (IOException e) {
			System.out.println("Impossible de trouver le fichier.");
		}

		System.out.println("###################### Test Negatif ################");
		try {

			// test negatif
			Dictionary dictionary = new Dictionary(new File("dicts/dictTest2.jsc"),useABR);
			//System.out.println(dictionary);
			System.out.println(dictionary.contains("ABNF"));
			System.out.println(dictionary.contains("abalones"));
			dictionary.add("test");
			System.out.println(dictionary.contains("test"));

			//dictionary.save();


		} catch (IOException e) {
			System.out.println("Impossible de trouver le fichier.");
		}
	}

}
